package com.jdbc.utils;

import com.jdbc.cms.javabean.Customer;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * JDBC Part6 测试 BaseDao
 * Description: 通过继承BaseDao获取其方法，执行CRUD操作
 * TODO：业务侧只需编写SQL语句，调用BaseDao方法
 */
public class BaseDaoTest extends BaseDao{

    /**
     * 插入一条用户数据
     */
    @Test
    public void testInsert() throws Exception{

        String sql = "insert into t_user(account,password,nickname) values (?,?,?);";

        int rows = executeUpdate(sql, "test333", "test333", "test333");

        if (rows > 0) {
            System.out.println("数据插入成功");
        } else {
            System.out.println("数据插入失败！");
        }
    }

    /**
     * 修改一条用户数据
     */
    @Test
    public void testUpdate() throws Exception{

        String sql = "update t_user set nickname = ? where id = ? ;";

        int rows = executeUpdate(sql, "test22", "10");

        if (rows > 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败！");
        }
    }

    /**
     * 删除一条用户数据
     */
    @Test
    public void testDelete() throws Exception{

        String sql = "delete from t_user where id = ? ;";

        int rows = executeUpdate(sql, "15");

        //输出结果
        if (rows > 0) {
            System.out.println("数据删除成功");
        } else {
            System.out.println("数据删除失败！");
        }
    }

    /**
     * 查询全部数据，并将数据存到List<Customer>
     * 请参考jdbc-cms模块代码
     */
    @Test
    public void testQueryMap() throws Exception {

        String sql = "select * from t_customer where id in (?, ?);";

        List<Customer> customerList = executeQuery(Customer.class, sql, 1, 4);
    }

}
